﻿#region header

//  ***************************************************************************
// *                                LICENSE
//  ***************************************************************************
//  * "Haplous" .NET Simple 2D Game Framework
//  * 
//  * Copyright © Jonathan Evans 2014-2014
//  * 
//  * This program is free software; you can redistribute it and/or
//  * modify it under the terms of the GNU General Public License
//  * as published by the Free Software Foundation; either version 2
//  * of the License, or (at your option) any later version.
//  * 
//  * This program is distributed in the hope that it will be useful,
//  * but WITHOUT ANY WARRANTY; without even the implied warranty of
//  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  * GNU General Public License for more details.
//  * 
//  * You should have received a copy of the GNU General Public License
//  * along with this program; if not, write to the Free Software
//  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//  * 
//  ***************************************************************************
//  * 
//  * Contact: opencollar@googlemail.com
//  * 
//  * Website: http://haplous.codeplex.com/
//  *
//  * NuGet:   http://www.nuget.org/packages/haplous/
//  *
//  * File:    Haplous/Ghosthunt/GhosthunterDirector.cs
//  *
//  ***************************************************************************

#endregion

using Haplous;

namespace Ghosthunt
{
    /// <summary>
    ///     A director for the Ghosthunter actor.
    /// </summary>
    internal class GhosthunterDirector : KeyboardDirector
    {
        /// <summary>
        ///     Initializes a new instance of the <see cref="ElementBase" /> class.
        /// </summary>
        /// <param name="game">
        ///     The game to which the class belongs.  The <paramref name="game" /> must not be
        ///     <see langword="null" />.
        /// </param>
        /// <param name="name">
        ///     The unique name that can be used to identify this object.  The <paramref name="name" /> must not be
        ///     <see langword="null" />, empty or all whitespace.
        /// </param>
        /// <param name="cue">The Cue that will be used to trigger movements.</param>
        /// <param name="stepSize">The size of each movement (in game pixels). Must be greater than zero.</param>
        /// <exception cref="System.ArgumentOutOfRangeException"><paramref name="stepSize" /> must be greater than zero. </exception>
        /// <param name="actor">The Actor to control.  Must not be <see langword="null" />.</param>
        public GhosthunterDirector(Game game, string name, Actor actor, Cue cue, int stepSize) : base(game, name, actor, cue, stepSize)
        {}

        /// <summary>
        ///     Ensures that a relative move to the current Actor position remains inside the game area.
        /// </summary>
        /// <param name="relativePosition">The relative position to which the Actor will move (relative to current position).</param>
        protected override void ConfineToGameArea(PositionInt32 relativePosition)
        {
            var newX = Actor.Position.X + relativePosition.X;
            var newY = Actor.Position.Y + relativePosition.Y;

            // Going off the left?
            if(newX < 0)
            {
                // Are we in "the gap" in the middle of the left or right of the maze?
                // If so, let the character "teleport" between sides.
                if((newY >= 600) && (newY <= 700))
                    relativePosition.X = (Game.Size.Width - Actor.Size.Width) - Actor.Position.X;
            }
            else
            {
                // Going off the right?
                if((newX + Actor.Size.Width) > Game.Size.Width)
                {
                    // Are we in "the gap" in the middle of the left or right of the maze?
                    // If so, let the character "teleport" between sides.
                    if((newY >= 600) && (newY <= 700))
                        relativePosition.X = -Actor.Position.X;
                }
            }

            base.ConfineToGameArea(relativePosition);
        }
    }
}